import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter
} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import {
  TechRuleListDto,
  TechRuleServiceProxy
} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

@Component({
  selector: 'TechRuleSameModal',
  templateUrl: './techrule-same-modal.component.html'
})
export class TechRuleSameModalComponent extends AppComponentBase {
  @ViewChild('createOrEditModal') modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  techrule = new TechRuleListDto();

  sametechs = new Array<TechRuleListDto>();

  constructor(
    injector: Injector,
    private _techruleServiceProxy: TechRuleServiceProxy
  ) {
    super(injector);
  }

  show(techrule: TechRuleListDto): void {
    this.techrule = techrule;
    this.active = true;
    this.modal.show();
    this.getTechRuleSames();
  }

  getTechRuleSames() {
    this.primengTableHelper.showLoadingIndicator();
    this._techruleServiceProxy
      .getSameTechRules(
        this.techrule.id,
        this.techrule.techRuleInfo.fileCode
      )
      .subscribe(result => {
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  goCar(techrule: TechRuleListDto, type: number) {
    this._techruleServiceProxy.goCar(techrule.id, type).subscribe(() => {
      this.getTechRuleSames();
      this.notify.success('加入成功');
    });
  }

  cancelCar(techrule: TechRuleListDto) {
    this._techruleServiceProxy.cancelCar(techrule.id).subscribe(() => {
      this.getTechRuleSames();
      this.notify.success('取消成功');
    });
  }

  deleteTechRule(techrule: TechRuleListDto): void {
    this.message.confirm(
      '确认删除该工规(' + techrule.techRuleInfo.fileCode + ')',
      this.l('AreYouSure'),
      isConfirmed => {
        if (isConfirmed) {
          this._techruleServiceProxy.deleteTechRule(techrule.id).subscribe(() => {
            this.getTechRuleSames();
            this.notify.success(this.l('SuccessfullyDeleted'));
          });
        }
      }
    );
  }

  close(): void {
    this.active = false;
    this.modalSave.emit(null);
    this.modal.hide();
  }
}
